package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.TermClassPriceDao;
import com.example.domain.TermClassPrice;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.TermClassPriceService;
import com.example.tool.DateTool;

@Service
public class TermClassPriceServiceImpl implements TermClassPriceService {

    @Autowired
    TermClassPriceDao termClassPriceDao;

    @Override
    public Result addTermClassPrice(TermClassPrice termClassPrice) {
        // TODO 添加学期班级学费信息
        if (termClassPrice != null) {
            // 获取系统当前时间作为记录创建时间
            termClassPrice.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        TermClassPrice termClassPrice1 = termClassPriceDao.save(termClassPrice);

        if (termClassPrice1 != null) {
            return ResultFactory.buildSuccessResult(termClassPrice1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result updateTermClassPrice(TermClassPrice termClassPrice) {
        // TODO 更新学期班级学费信息
        if (termClassPrice != null) {
            // 获取系统当前时间作为记录更新时间
            termClassPrice.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        TermClassPrice termClassPrice1 = termClassPriceDao.saveAndFlush(termClassPrice);

        if (termClassPrice1 != null) {
            return ResultFactory.buildSuccessResult(termClassPrice1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result deleteTermClassPriceById(String id) {
        // TODO 根据学期班级学费编号删除学期班级学费信息
        int row = termClassPriceDao.deleteByTermClassPriceId(Integer.parseInt(id));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result queryAllTermClassPrice() {
        // TODO 查询所有学期班级学费信息
        List<TermClassPrice> list = termClassPriceDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result queryTermClassPriceById(String id) {
        // TODO 根据编号查询学期班级学费信息
        List<TermClassPrice> list = termClassPriceDao.findByTermClassPriceId(Integer.parseInt(id));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result addTermClassPriceByExcel(List<TermClassPrice> list) {
        // TODO 通过Excel批量添加学期班级学费信息
        List<TermClassPrice> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = termClassPriceDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result deleteTermClassPriceByIds(Map<String, String> ids) {
        // TODO 根据学期班级学费编号批量删除学期班级学费信息
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = termClassPriceDao.deleteByTermClassPriceIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result serachByInfo(String[] info) {
        // TODO 学期班级学费信息模糊查询
        List<Map<String, Object>> list = null;
//		System.out.println(info[0]);
        if (info[0].equals("termName")) {

            list = termClassPriceDao.findByTermNameLike(info[1]);

        } else if (info[0].equals("className")) {

            list = termClassPriceDao.findByClassNameLike(info[1]);

        } else if (info[0].equals("price")) {

            list = termClassPriceDao.findByPriceLike(info[1]);

        }

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result queryAllTermClassPriceInfo() {
        // TODO 查询所有学期班级学费信息
        List<Map<String, Object>> list = termClassPriceDao.queryAllTermClassPriceInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result queryAllTermClassInfo() {
        // TODO 查询所有学期和班级信息
        List<Map<String, Object>> list = termClassPriceDao.queryAllTermClassInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result queryAllTermClassNameInfo() {
        // TODO 查询所有学期和班级信息
        List<Map<String, Object>> list = termClassPriceDao.queryAllTermClassNameInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
